package com.easypan.controller;

import com.easypan.Service.FileInfoService;
import com.easypan.Service.UserService;
import com.easypan.annotation.GlobalInterceptor;
import com.easypan.entity.dto.SessionwebUserDto;
import com.easypan.entity.po.UserInfo;
import com.easypan.entity.query.FileInfoQuery;
import com.easypan.entity.query.UserInfoQuery;
import com.easypan.entity.vo.PaginationResultVO;
import com.easypan.entity.vo.R;
import com.easypan.entity.vo.UserInfoVO;
import com.easypan.util.CopyTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/admin")

public class adminController extends CommonFileController{
@Autowired
    UserService userService;
@Autowired
    FileInfoService fileInfoService;
@PostMapping("/loadUserList")
    public R loadFileList(UserInfoQuery userInfoQuery,HttpSession session)
{
    SessionwebUserDto webUserDto = (SessionwebUserDto) session.getAttribute("session_key");
    PaginationResultVO resultVO=   userService.loadFileList(webUserDto,userInfoQuery);
    return R.success(convert2PaginationVO(resultVO, UserInfoVO.class));
}
    /**
     * 这是将PaginationResultVO对象中的信息进一步简化为FileInfoVO进行返回页面的
     * @param result PaginationResultVO传入的对象，
     * @param classz 这是需要返回的文件类型，使用反射机制获取
     * @return
     * @param <S>
     * @param <T>
     */
    protected <S, T> PaginationResultVO<T> convert2PaginationVO(PaginationResultVO<S> result, Class<T> classz) {
        PaginationResultVO<T> resultVO = new PaginationResultVO<>();
        resultVO.setList(CopyTools.copyList(result.getList(), classz));
        resultVO.setPageNo(result.getPageNo());
        resultVO.setPageSize(result.getPageSize());
        resultVO.setPageTotal(result.getPageTotal());
        resultVO.setTotalCount(result.getTotalCount());
        return resultVO;
    }

    ///too 文件列表显示失败
@PostMapping("/loadFileList")
    //POST
    //
    //scheme
    //	http
    //host
    //	easypan.wuhancoder.com
    //filename
    //	/api/admin/loadFileList
    public  R loadFileList(FileInfoQuery query)
{

      //分页查询文件
    query.setOrderBy("last_update_time desc");


    PaginationResultVO resultVO = fileInfoService.findListByPage(query);
    return R.success(resultVO);

}

    @RequestMapping("/getFolderInfo")

    public R getFolderInfo( String path) {
        return super.getFolderInfo(path, null);
    }


    /**
     * 这里是根据管理员获取的用户信息，因此需要传递用户id
     * @param response
     * @param userId
     * @param fileId
     */
    /// getFile(HttpServletResponse httpServletResponse, String mp4Info, HttpSession session)
    @RequestMapping("/getFile/{userId}/{fileId}")
    public R getFile(HttpServletResponse response, String userId,
                     String fileId) {

        super.getFile(response, fileId, userId);
       return R.success("成功");
    }

    /**
     * 获取视频
     * @param response
     * @param userId
     * @param fileId
     */
    @RequestMapping("/ts/getVideoInfo/{userId}/{fileId}")

    public void getVideoInfo(HttpServletResponse response,
                             String userId,
                             String fileId) {
        super.getFile(response, fileId, userId);
    }


    @RequestMapping("/createDownloadUrl/{userId}/{fileId}")

    public R createDownloadUrl( String userId,
                                       String fileId) {
        return fileInfoService.createDownloadUrl(fileId,userId);
    }

    /**
     * 下载
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/download/{code}")

    public void download(HttpServletRequest request, HttpServletResponse response,
                       String code) throws Exception {
        ///fileInfoService.download(request);
    }


    /**
     * 删除文件，这是管理员进行的操作，前端传递过来的是userId_fileId,userId1_fileId1,userId2_fileId2这种形式，
     * 因此需要先将各组文件分开在进行，在进行删除操作
     * @param fileIdAndUserIds
     * @return
     */
    @RequestMapping("/delFile")

    public R delFile(String fileIdAndUserIds) {
        return null;
    }

}
